﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpSvn;

namespace GestoreSuperProgetto
{
    public partial class SelectionReleaseFileFrom : Form
    {
        private List<SharpSvn.SvnStatusEventArgs> statusList = new List<SharpSvn.SvnStatusEventArgs>();
        public SelectionReleaseFileFrom()
        {
            InitializeComponent();
        }

        public SvnClient ClientSvn { get; set; }

        public void ReciveStatusFile(object sender, SharpSvn.SvnStatusEventArgs args)
        {
            if (args.LocalContentStatus != SharpSvn.SvnStatus.Normal)
            {
                statusList.Add(args);
                statusList.Sort((ele, ele2) =>
                {
                    //if (ele.NodeKind.CompareTo(ele2.NodeKind) == 0)
                    //    return ele.LocalContentStatus.CompareTo(ele2.LocalContentStatus);
                    return -ele.LocalContentStatus.CompareTo(ele2.LocalContentStatus);
                });
                listFileDataGridView.DataSource = (from status in statusList
                                            where status.LocalContentStatus != SharpSvn.SvnStatus.NotVersioned
                                            select status).ToList();
            }
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for (int rowindex = e.RowIndex; rowindex < e.RowIndex + e.RowCount; rowindex++)
            {
                SharpSvn.SvnStatusEventArgs val = (listFileDataGridView.Rows[rowindex].DataBoundItem as SharpSvn.SvnStatusEventArgs);
                if (val.LocalContentStatus == SharpSvn.SvnStatus.Modified)
                {
                    listFileDataGridView[0, rowindex].Value = true;
                    foreach (var cell in listFileDataGridView.Rows[rowindex].Cells)
                        (cell as DataGridViewCell).Style.ForeColor = Color.DarkBlue;
                }
                else if (val.LocalContentStatus == SharpSvn.SvnStatus.Deleted ||
                         val.LocalContentStatus == SharpSvn.SvnStatus.Missing)
                {
                    foreach (var cell in listFileDataGridView.Rows[rowindex].Cells)
                        (cell as DataGridViewCell).Style.ForeColor = Color.Red;
                }
                else
                {
                    foreach (var cell in listFileDataGridView.Rows[rowindex].Cells)
                        (cell as DataGridViewCell).Style.ForeColor = Color.Black;
                }
            }
        }

        private void unversionedChk_CheckedChanged(object sender, EventArgs e)
        {
            if (unversionedChk.Checked)
            {
                listFileDataGridView.DataSource = statusList;
            }
            else
            {
                listFileDataGridView.DataSource = (from status in statusList
                                            where status.LocalContentStatus != SharpSvn.SvnStatus.NotVersioned
                                            select status).ToList();
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (listFileDataGridView.SelectedRows.Count == 1)
            {
                var status = listFileDataGridView.SelectedRows[0].DataBoundItem as SvnStatusEventArgs;

                if (status.LocalContentStatus == SvnStatus.Modified)
                    revertToolStripMenuItem.Visible = true;
                else
                    revertToolStripMenuItem.Visible = false;

                if (status.LocalContentStatus == SvnStatus.Missing)
                    cancellaToolStripMenuItem.Visible = true;
                else
                    cancellaToolStripMenuItem.Visible = false;
                
                if (status.LocalContentStatus == SvnStatus.NotVersioned)
                    aggiungiToolStripMenuItem.Visible = true;
                else
                    aggiungiToolStripMenuItem.Visible = false;
            }
        }

        private void aggiungiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listFileDataGridView.SelectedRows.Count == 1)
            {
                var status = listFileDataGridView.SelectedRows[0].DataBoundItem as SvnStatusEventArgs;
                ClientSvn.Add(status.FullPath);
            }
        }

        private void cancellaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listFileDataGridView.SelectedRows.Count == 1)
            {
                var status = listFileDataGridView.SelectedRows[0].DataBoundItem as SvnStatusEventArgs;
                ClientSvn.Delete(status.FullPath);
            }
        }

        private void revertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listFileDataGridView.SelectedRows.Count == 1)
            {
                var status = listFileDataGridView.SelectedRows[0].DataBoundItem as SvnStatusEventArgs;
                ClientSvn.Revert(status.FullPath);                
            }
        }
    }
}
